<?php

namespace app\admins\controller;

use think\Controller;
use Util\SysDb;

class Book extends Controller
{
    public function bookadd()
    {
        $uid = input('param.userId');
        $teachername = input('param.teachername');
        $bookName = input('param.bookName');
        $bookabstract = input('param.bookabstract');
        $bookImg = input('param.bookImg');
        $classify = input('param.classify');
        $Content = input('param.content');
        $this->db = new SysDb();
        $count = $this->db->table('booklist')->where(['uid' => $uid])->count();
        $data = [];

        $data['uid'] = $uid;
        $count = $count + 1;
        $data['pid'] = $count;
        $data['bookName'] = $bookName;
        $data['bookabstract'] = $bookabstract;
        $data['bookImg'] = $bookImg;
        $data['Connect'] = $Content;
        $data['classify'] = $classify;
        $data['Teacher']=$teachername;
        $add = $this->db->table('booklist')->insert($data);
        if ($add) {
            exit(json_encode(['code' => 0, 'msg' => '添加成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '添加失败']));
        }
    }

    public function bookEdit()
    {
        $uid = input('param.userId');
        $pid = input('param.pid');
        $bookName = input('param.bookName');
        $bookabstract = input('param.bookabstract');
        $bookImg = input('param.bookImg');
        $classify = input('param.classify');
        $Connect = input('param.bookConnect');
        
        $this->db = new SysDb();

        $data = [];
        $data['pid'] = $pid;
        $data['uid'] = $uid;
        $data['Connect'] = $Connect;
        $data['bookName'] = $bookName;
        $data['bookabstract'] = $bookabstract;
        $data['classify'] = $classify;
        $data['bookImg'] = $bookImg;

        $update = $this->db->table('booklist')->where(['uid' => $uid])->where(['pid' => $pid])->update($data);
        if ($update) {
            exit(json_encode(['code' => 0, 'msg' => '添加成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '添加失败']));
        }
    }

    public function bookDel()
    {
        $pid = input('param.pid');
        $uid = input('param.userId');

        $this->db = new SysDb();
        $del = $this->db->table('booklist')->where(['uid' => $uid])->where(['pid' => $pid])->delete();
        if ($del) {
            $resetId = $this->db->table('booklist')->where(['uid' => $uid])->lists();
            $re = 1;

            foreach ($resetId as $rid) {
                if ($re < 10) {
                    $re = '0'.$re;
                }
                $this->db->table('booklist')->where(['uid' => $uid])->where(['pid' => $rid['pid']])->update(['pid' => $re.'_'.$uid]);
                ++$re;
            }
            $re = 1;
            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }

    public function bookList()
    {
        $uid = input('param.userId');
        $this->db = new SysDb();
        $data1 = $this->db->table('booklist')->where(['uid' => $uid])->lists();
        if ($data1 != null) {
            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            // $index['count'] = $count;
            $index_json = json_encode($index);

            exit($index_json);
        } else {
            exit(json_encode(['code' => 0, 'msg' => '暂时没有图书']));
        }
        // $count = $this->db->table('booklist')->where(['uid' => $uid])->count();
    }

    public function bookAllDel()
    {
        $allId = input('param.allId');
        $uid = input('param.userId');

        $this->db = new SysDb();
        foreach ($allId as $id) {
            $del = $this->db->table('booklist')->where(['pid' => $id])->delete();
        }
        if ($del) {
            $resetId = $this->db->table('booklist')->where(['uid' => $uid])->lists();
            $re = 1;
            foreach ($resetId as $rid) {
                if ($re < 10) {
                    $re = '0'.$re;
                }
                $this->db->table('booklist')->where(['uid' => $uid])->where(['pid' => $rid['pid']])->update(['pid' => $re.'_'.$uid]);
                ++$re;
            }
            $re = 1;
            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }

    public function StundentBook(){
        $this->db = new SysDb();
        $nojs=[];
        $php=[];
        $vuejs=[];
        $other=[];
        $data1 = $this->db->table('booklist')->lists();
        if ($data1 != null) {
            $center = [];
           
            foreach ($data1 as $d) {
                if($d['classify']=="no.js"){
                    array_push($nojs, $d);
                }
                if($d['classify']=="php"){
                    array_push($php, $d);
                }
                if($d['classify']=="vue.js"){
                    array_push($vuejs, $d);
                }
                if($d['classify']=="其他"){
                    array_push($other, $d);
                }

            }
            $center['no.js'] =  $nojs;
            $center['php'] =  $php;
            $center['vue.js'] =  $vuejs;
            $center['其他'] =  $other;
            $index['data'] = $center;
            // $index['count'] = $count;
            $index_json = json_encode($index);

            exit($index_json);
        } else {
            exit(json_encode(['code' => 0, 'msg' => '暂时没有图书']));
        }
    }
}
